/// @FUNCTION
/// @NAME XfHashGet
/// @DESC 
/// @ACCESS Server
/// @MP False
/// @PARAM Array The hash table
/// @PARAM String The name of the key in the hash you want to retrieve
/// @PARAM Variant The default value that you expect out of the method if the key is not found
/// @RETURN The value contained in the hash or the default
/// @REQUIRE Nothing
/// @REMARK
/// @EXAMPLE [_instances, "Reinforcements", []] call CFUNC(HashGet);

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "arrays\fn_hashGet.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>
#ifndef __DUMP_INDENT__
#define __DUMP_INDENT__	"    "
#endif

private ["_hash_array","_hash_key","_hash_indexer","_index","_hash_values","_result","_default"];
TRACE_1("Start",_this);
TMFPROFILERSTART;
If (count _this > 2) then {	_default = _this select 2; };
If (typeName _this == typename []) then {
	if (count _this >= 2) then {
		_hash_array = _this select 0;
		_hash_key = _this select 1;
		if ((typeName _hash_array == typename[]) && (typeName _hash_key == typename "")) then {
			// For safety and easyness, we Upper all keys internally
			_hash_key = toUpper _hash_key;
			// Normally "== 2" is enough since a hash only needs a key indexer and a value array, but in the future we might extend it to more keys for multi dimensional storage
			//  So they 1st dimension keys array is always the first and the values container is always the last one
			if (count _hash_array >= 2) then {
				_hash_indexer = _hash_array select 0;
				_hash_values = _hash_array select (count _hash_array - 1);
				_index = _hash_indexer find _hash_key;
				if (_index >= 0 && _index < count _hash_values) then {
					_result = _hash_values select _index;
				};
			};				
		};
	};
};
if ((isNil "_result") && !(IsNil "_default")) then { _result = _default };
TRACE_1("End",_result);
TMFPROFILERSTOP;
_result;